<template>
  <Container class="single-sale">
    <div slot="header" class="container-header title title-center">
      满减促销
      <Button size="large" class="fl btn-back" icon="ios-arrow-back" @click="jumpPrevPage">返回</Button>
    </div>
    <div class="reduce-promotion">
      <!-- 基础设置 -->
      <DefaultEditItem
        ref="fullSubForm"
        :form="$store.state.fullsubSale.FullsubForm"
        :rules="rules"
      />
    </div>
    <div slot="footer" class="container-footer bg-white">
      <Button type="primary" size="large" class="ml" @click="onShowList">下一步</Button>
    </div>
  </Container>
</template>
<script>
import DefaultEditItem from './edit/DefaultEditItem'
export default {
  components: {
    DefaultEditItem
  },
  data () {
    const validatemain = function (rule, value, callback) {
      if (!value) {
        return callback('请输入减金额')
      } else if (!/^\d+(\.\d{1,2})?$/.test(value)) {
        return callback(new Error('非负数且最多两位小数'))
      } else {
        callback()
      }
    }
    return {
      id: this.$route.query.id || '',
      type: this.$route.query.type || '',
      isEmpty: false, // 是否清空数据
      isSave: true, // 是否保存数据
      model1: '',
      value: '',
      selectType: false,
      rules: {
        name: [{ required: true, message: '请输入促销名称', trigger: 'blur' }],
        date: [
          {
            required: true,
            message: '请开始时间',
            trigger: 'change',
            type: 'array'
            // fields: {
            //   0: { type: "date", required: true, message: "请输入起止日期" },
            //   1: { type: "date", required: true, message: "请输入起止日期" }
            // }
          }
        ],
        type: [
          { required: true, message: '请输入促销类型', trigger: 'change' }
        ],
        grade: [
          {
            required: true,
            type: 'array',
            message: '请选择活动对象',
            trigger: 'change'
          }
        ],
        gender: [
          { required: true, message: '请选择参与方式', trigger: 'blur' }
        ],
        main: [{ validator: validatemain, trigger: 'blur' }],
        second: [{ validator: this.validatesecond, trigger: 'blur' }]
      }
    }
  },
  computed: {
    gender (val) {
      let word
      if (this.formValidate.gender == '1') {
        word = '活动商品'
      } else if (this.formValidate.gender == '2') {
        word = '不参加活动的商品'
      }
      return word
    }
  },
  mounted () {
    // this.id && this.getDetailData()
  },
  methods: {
    getDetailData () {
      this.$ajaxGet('/activity/full_cut/detail/' + `${this.id}`).then(data => {
        this.$store.state.fullsubSale.FullsubForm.remarks = data.activityInfoDTO.activityDescription//活动备注
        this.$store.state.fullsubSale.FullsubForm.name = data.activityInfoDTO.activityName //促销名称
        this.$store.state.fullsubSale.FullsubForm.grade = this.promotionMemberScope = data.activityInfoDTO.storeTypeEnumList //活动对象
        this.$store.state.fullsubSale.FullsubForm.date = [new Date(data.activityInfoDTO.startDate), new Date(data.activityInfoDTO.endDate)]
        this.$store.state.fullsubSale.FullsubForm.type = data.activityFullCutDTO.fullCutTypeEnum == 'STEP_FULL_CUT' ? '1' : '2' // 促销类型
        let gender = ''
        if (data.activityInfoDTO.joinTypeEnum == 'WHOLE') {
          gender = '1'
        } else if (data.activityInfoDTO.joinTypeEnum == 'PART') {
          gender = '2'
        } else {
          gender = '3'
        }
        this.$store.state.fullsubSale.FullsubForm.gender = gender
        // fullCutRuleList -- 促销规则
        if (data.activityFullCutDTO.fullCutTypeEnum == 'STEP_FULL_CUT') { // 阶梯递减
          this.$store.state.fullsubSale.FullsubForm.discount1 = data.activityFullCutDTO.fullCutRuleList.map(item => {
            return {
              value: item.m,
              num: '',
              isParcel: item.isParcel
            }
          })
          this.$store.state.fullsubSale.FullsubForm.discount2 = data.activityFullCutDTO.fullCutRuleList.map(item => {
            return {
              value: item.n,
              num: ''
            }
          })
        } else {
          this.$store.state.fullsubSale.FullsubForm.main = data.activityFullCutDTO.fullCutRuleList[0].m
          this.$store.state.fullsubSale.FullsubForm.second = data.activityFullCutDTO.fullCutRuleList[0].n
          this.$store.state.fullsubSale.FullsubForm.isParcel = data.activityFullCutDTO.fullCutRuleList[0].isParcel
        }
      })
    },
    validatesecond (rule, value, callback) {
      if (!value) {
        return callback('请输入满金额')
      } else if (!/^\d+(\.\d{1,2})?$/.test(value)) {
        return callback(new Error('非负数且最多两位小数'))
      } else {
        if (value / this.$store.state.fullsubSale.FullsubForm.main >= 0.8) {
          return callback(new Error('减金额最大为满金额的80%'))
        } else {
          callback()
        }
      }
    },
    onShowList () {
      let self = this
      this.$refs['fullSubForm'].$refs['form'].validate(valid => {
        if (valid) {
          let url = { name: 'sales/fullsub/list', query: {} }
          this.$Message.success('保存成功!')
          if (self.id) {
            url.query.id = self.id
            if (this.type) url.query.type = self.type
          }
          self.$router.push(url)
        }
      })
    },
    jumpPrevPage () {
      this.$router.back()
    },
    choiceSelect (val) {
      switch (val) {
        case '1':
          this.selectType = false
          break
        case '2':
          this.selectType = true
          break
      }
    },
    RadioType (val) {
      switch (val) {
        case '1':
          break
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    /*
        1. 路由从不是下一页进入的都清空
        2. 路由从下一页进入并且有id就不刷新接口--保存信息
      */
    next(vm => {
      if (from.name !== 'sales/fullsub/list') {
        vm.isEmpty = true
      } else {
        vm.isSave = false
      }
      if (vm.isEmpty) vm.$store.commit('setFullSubEmpty')
      if (vm.id && vm.isSave) vm.getDetailData()
    })
  }
}
</script>

